package cn.zn.wx.mq;

import cn.hutool.core.util.StrUtil;
import cn.zn.framework.common.utils.Constant;
import cn.zn.modules.zzsys.cur.service.SysWxmpService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * <pre>
 * @projectName: finance-all
 * @packageName: cn.zn.wx.mq
 * @className: MqConsumers
 * @description: 消息队列消费者
 * @author: zn
 * @date: 2021/7/23 10:05
 * </pre>
 */
@Component
@Slf4j
public class MqConsumers {
    @Autowired
    private SysWxmpService sysWxmpService;

    /**
     * APP操作日志
     *
     * @param message
     * @param channel
     */
    @RabbitHandler
    @RabbitListener(queuesToDeclare = @Queue(Constant.MQ_PZ_PROCESS_EVENT_WEIXIN))
    public void pzProcessEvent(Message message, Channel channel) {
        try {
            // 接收到的字符形消息
            String body = new String(message.getBody());
            log.debug("message:{}", message);
            log.debug("body:{}", body);
            if (StrUtil.isNotBlank(body)) {
                sysWxmpService.pushTmpMsgPzProcessEnent(body);
            } else {
                log.error("消息未推送，因为消息内容为空");
            }
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
        } catch (Exception e) {
            log.error("消费凭证流程事件消息错误：{}", e.getMessage(), e);
        }
    }
}
